package com.storeapp.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import com.storeapp.entity.domain.Product;
import com.storeapp.service.ProductService;
import com.storeapp.util.SpringUtils;

public class ProductList extends ViewPart implements IPropertyChangeListener {
	public static final String ID = "com.storeapp.ui.ProductList";
	
	private ListViewer lv;
	private List<Product> products;
	private ProductService productService;
	private Action addAction;
	private Action deleteAction;
	private Action updateAction;
	
	class ViewContentProvider implements IStructuredContentProvider {		
		@Override
		public void dispose() {
			// TODO Auto-generated method stub
		}
		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub
		}
		@SuppressWarnings("unchecked")
		@Override
		public Object[] getElements(Object inputElement) {
			// TODO Auto-generated method stub
			List<Product> list = (List<Product>) inputElement;
			return list.toArray();
		}		
	}
	
	class ViewLabelProvider extends LabelProvider {
		public String getText(Object obj) {
			Product product = (Product)obj;
			return product.getProductName();
		}
	}

	public ProductList(){
		super();
		this.products = new ArrayList<Product>();
		productService = (ProductService) SpringUtils
		.getApplicationContext().getBean("productService");
	}
	
	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		lv = new ListViewer(parent, SWT.BORDER | SWT.V_SCROLL);
		lv.setContentProvider(new ViewContentProvider());
		lv.setLabelProvider(new ViewLabelProvider());
		products = this.productService.listAll();
		lv.setInput(products);
		
		getSite().setSelectionProvider(lv);
		
		//create context menu
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
	}

	/**
	 * Create the menu manager.
	 * 
	 * @return the menu manager
	 */
	protected void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			@Override
			public void menuAboutToShow(IMenuManager manager) {
				fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(lv.getControl());
		lv.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, lv);
		getSite().setSelectionProvider(lv);
	}
	
	private void hookDoubleClickAction() {
		// TODO Auto-generated method stub
		
	}

	private void makeActions() {
		// TODO Auto-generated method stub
		//add
		addAction = new Action() {
			public void run() {
				try {
					ProductEditView.actionType="add";
					getSite().getWorkbenchWindow().getActivePage().showView(ProductEditView.ID);
				} catch (PartInitException e) {
					MessageDialog.openError(getSite().getWorkbenchWindow().getShell(), "Error", "Error opening view:" + e.getMessage());
				}
			}
		};
		addAction.setText("添加");
		addAction.setToolTipText("添加");
		addAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		//update
		updateAction = new Action() {
			public void run() {
				try {
					ProductEditView.actionType="update";
					getSite().getWorkbenchWindow().getActivePage().showView(ProductEditView.ID,null, IWorkbenchPage.VIEW_CREATE);
				} catch (PartInitException e) {
					MessageDialog.openError(getSite().getWorkbenchWindow().getShell(), "Error", "Error opening view:" + e.getMessage());
				}
			}
		};
		updateAction.setText("更新");
		updateAction.setToolTipText("更新");
		updateAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		//delete
		deleteAction = new Action() {
			public void run() {
				showMessage("Action delate executed");
			}
		};
		deleteAction.setText("删除");
		deleteAction.setToolTipText("删除");
		deleteAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
			getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		
		
//		doubleClickAction = new Action() {
//			public void run() {
//				ISelection selection = viewer.getSelection();
//				Object obj = ((IStructuredSelection)selection).getFirstElement();
//				showMessage("Double-click detected on "+obj.toString());
//			}
//		};
	}
	
	private void fillContextMenu(IMenuManager manager) {
		manager.add(addAction);
		manager.add(updateAction);
		manager.add(deleteAction);
		// Other plug-ins can contribute there actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}
	
	private void showMessage(String message) {
		MessageDialog.openInformation(
			lv.getControl().getShell(),
			"Sample View",
			message);
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent event) {
		// TODO Auto-generated method stub
		if( event.getProperty().equals("AddNewProduct")) {
			products.clear();
			products.addAll(productService.listAll());
			lv.refresh();
		} else {
			products.remove(event.getOldValue());
			products.add((Product) event.getNewValue());
			lv.refresh(event.getOldValue());
		}
	}
	
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		lv.getControl().setFocus();
	}
}
